<!-- Sofija Puric 6/08 -->
<?php
    require '../../pages/db.php';
    $con=dbConnect("sib");

   // $hid=$_GET["hid"];
    $hid=$_POST["hid"];
    $url=$_POST["url"];
    $alt=$_POST["alt"];
    $index_photo=$_POST["index_photo"];

    switch ($index_photo) {

                case 1: $index = 1; break;
                case 2: $index = 0; break;
            }

        $sql_index_photo = "SELECT * FROM photo WHERE IDHostel = '$hid' AND IndexPhoto = '1'";
        $result_index_photo = mysql_query($sql_index_photo, $con);
        if (!$result_index_photo)
            die('Error: ' . mysql_error());
        
    

   // echo !check_index_photo();


    if ($url=="" || $alt=="")
          echo '<div id="error">ERROR: You must fill in all the gaps!<div/>';
    else {
        if (($index == 1) && mysql_num_rows($result_index_photo))
          echo '<div id="error">ERROR: Index photo already exists!<div/>';
    
    //if (($url != "") && ($alt != "") && (($index == 0) || (($index == 1) && check_index_photo()))) {
        else {    $sql="INSERT INTO photo( Url, Alt, IDHostel, IndexPhoto)
            VALUES('$url', '$alt', '$hid', '$index')";

        if (!mysql_query($sql, $con)) 
             die('Error: ' . mysql_error());
        
        else 
             header('Location: ../hostels-admin.php');

         

    }
    }
 
    
    
    mysql_close($con);
?>
